package com.woniu.job.controller;

import com.woniu.job.entity.*;

import com.woniu.job.exception.service.AccountExistException;
import com.woniu.job.exception.service.ServiceException;
import com.woniu.job.service.ICompanyService;
import com.woniu.job.service.IResumeService;
import com.woniu.job.service.IResumeworkService;
import com.woniu.job.service.IWorkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: job
 * @description: UserController
 * @author: 福尔摩司
 * @create: 2019-12-24 19:57
 **/
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IWorkService workService;
    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IResumeworkService resumeworkService;

    /**
     * @Description:用户注册
     * @Param:
     * @return:
     * @Author: 福尔摩司
     * @Date: 2019/12/24 19:59
     */
    @RequestMapping("/reg")
    public String register() {
        return "register";
    }

    @RequestMapping("/regcomp")
    public String regcomp() {
        System.out.println("6666666666666666666");
        return "reg_comp";
    }
    @RequestMapping("/regc")
    public String register(Company company, Model model){
        try {
            companyService.register(company);

        } catch (ServiceException e) {
            if (e.getCause() instanceof AccountExistException) {
                model.addAttribute("msg", "用户名重复或不合法");
                return "reg_comp";
            } else {
                model.addAttribute("msg", "系统繁忙");
                return "reg_comp";
            }
        }
        return "login";
    }

    /**
     * @Description:用户登录
     * @Param:
     * @return:
     * @Author: 福尔摩司
     * @Date: 2019/12/25 15:10
     */
    @RequestMapping("/lgin")
    public String login() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(Company company, Model model,HttpSession session){
      if(company!=null){
          Company getCompany = companyService.getByAccount(company.getAccount());
          if (getCompany != null && StringUtils.equals(company.getPsw(), getCompany.getPsw())) {
              session.setAttribute("company",getCompany);
              model.addAttribute("company",getCompany);
              List<Work> works = workService.getWorksMap();
              List<Resume> resumes = resumeService.getResumes();
              List<Resume> resumest = resumeService.getResumes();
              for (Work work : works) {
                  String name = work.getCompanys().getName();
              }
              model.addAttribute("resumes", resumes);
              model.addAttribute("works", works);
              model.addAttribute("resumest", resumest);
              return "index";

          }
      }
      model.addAttribute("msg","账号或密码错误");
      return "login";
    }

    /**
     * @Description:职位搜索
     * @Param:
     * @return:
     * @Author: 福尔摩司
     * @Date: 2019/12/25 17:43
     */
    @RequestMapping("/select")
    public String select(Work work, Model model) {

        List<Work> works = companyService.select(work);
        model.addAttribute("works", works);
        System.out.println("5555555555555555555" + work);
        return "index";
    }

    /**
     * 补全主页信息
     * @return
     */
    @PostMapping("/message")
    public String addMessage(Company com,@SessionAttribute Company company){
        com.setId(company.getId());
        companyService.addMessage(com);
        return "company/index";
    }

    /**
     * 跳转补全企业信息页面
     * @return
     */
    @GetMapping("/tomessage")
    public String toMessage(){
        return "company/profile";
    }

    /**
     * 跳转公司主页面
     *
     * @Author: 陈思宇
     */
    @RequestMapping("/index")
    public String index() {
        return "company/index";
    }

    /**
     * 跳转公司LOGO
     *
     * @Author: 陈思宇
     */
    @RequestMapping("/logo")
    public String logo() {
        return "company/logo";
    }

    /**
     * 跳转企业营业执照
     *
     * @Author: 陈思宇
     */
    @RequestMapping("/auth")
    public String auth() {
        return "company/auth";
    }

    /**
     * 跳转企业地图
     *
     * @Author: 陈思宇
     */
    @RequestMapping("/map")
    public String map() {
        return "company/map";
    }



    /**
     * 营业执照的上传
     * @param img
     * @return 是否成功
     */
    @RequestMapping({"/permits"})
    public String permits(HttpSession session, String img) {
        Company company = (Company) session.getAttribute("company");
        String id = company.getId();
        Company company2 =companyService.getById(id);
        if(company2.getPermit() != null && company2.getDescription() != null && company2.getLegalPerson() != null){
            company2.setStatus("1");
            companyService.updateById(company2);
        }

        company2.setPermit(img);
        companyService.updateById(company2);

        Company company3 =companyService.getById(id);
        session.removeAttribute("company");
        session.setAttribute("company",company3);
        return "company/auth";
    }

    /**
     * logos的上传
     * @param logo
     * @return 是否成功
     */
    @RequestMapping({"/logos"})
    public String logo(HttpSession session, String logo) {
        Company company = (Company) session.getAttribute("company");
        String id = company.getId();
        Company company1 = new Company();
        company1.setId(id);
        company1.setLogo(logo);
        companyService.updateById(company1);
        Company company2 =companyService.getById(id);
        session.removeAttribute("company");
        session.setAttribute("company",company2);
        return "company/index";
    }

    @GetMapping("/getresumework/{page}")
    public String getResume(Model model, @SessionAttribute(required = false) Company company, @PathVariable("page") Integer page){
        if (page==null || page==0){
            page=1;
        }
        if (company!=null){
            PageBean<Resumework> resumeworkPageBean =  resumeworkService.getResumeByCompany(company.getId(),page);
            model.addAttribute("myresume",resumeworkPageBean);
        }

        return "company/resume_down";
    }

}
